/*************************************************************************** 
 * (c) 2006 Dmitry Osinovsky, osdmsoftware@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 ***************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Resources;
using Microsoft.Win32;
using OsdmSoftware.CommonLibrary;
using System.IO;

namespace OsdmSoftware.PocketPicture
{
    public class AboutAction: Action
    {
        public AboutAction()
        {
            this.Name = "About";
            this.ImageName = "Info";
            this.ParentName = "File2";
            this.Hint = "Show info about this program and its authors";
        }

        public override void DoAction()
        {
            AboutDialog ad = new AboutDialog();
            ad.ShowDialog();
            ad.Dispose();
            base.DoAction();
        }
    }

    public class NewAction: Action
    {
        public NewAction()
        {
            this.Name = "New";
            this.ImageName = "NewAction";
            this.ParentName = "File1";
            this.Hint = "Create new image";
        }

        public override void DoAction()
        {
            if (!Form.CheckSaved())
                return;
            SizeForm sf = new SizeForm();

            sf.WidthEdit = Convert.ToInt32(Registry.GetValue(PocketPictureForm.RegKey, "ImageWidth", 32));
            sf.HeightEdit = Convert.ToInt32(Registry.GetValue(PocketPictureForm.RegKey, "ImageHeight", 32));
            sf.ResizeCheck.Visible = false;
            if (sf.ShowDialog() == DialogResult.OK)
            {
                Registry.SetValue(PocketPictureForm.RegKey, "ImageWidth", sf.WidthEdit);
                Registry.SetValue(PocketPictureForm.RegKey, "ImageHeight", sf.HeightEdit);
                Form.NewBitmap(sf.WidthEdit, sf.HeightEdit);
            }
            base.DoAction();
        }
    }

    public class OpenAction: Action
    {
        public OpenAction()
        {
            this.Name = "Open";
            this.ImageName = "OpenAction";
            this.ParentName = "File1";
            this.Hint = "Open existing image";
        }

        public override void DoAction()
        {
            if (!Form.CheckSaved())
                return;

#if PocketPC
            if (Form.UseStandartFileDialog)
#endif
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();

                openFileDialog1.InitialDirectory = "My Pictures";
                openFileDialog1.Filter = "Image Files (*.BMP;*.JPG;*.PNG;*.GIF)|*.bmp;*.jpg;*.gif;*.png|All files (*.*)|*.*";

                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    Form.OpenFile(openFileDialog1.FileName);
                }
            }
#if PocketPC
            else
            {
                string lastDirectory = (string) Registry.GetValue(PocketPictureForm.RegKey,
                    "LastDirectory", "\\My Documents");

                CompleteFileDialog openFileDialog1 = new CompleteFileDialog();
                openFileDialog1.PreviewControl = new FileDialogPicturePreview();
                openFileDialog1.InitialDirectory = lastDirectory;
                openFileDialog1.Filter = "Image Files (*.BMP;*.JPG;*.PNG;*.GIF)|*.bmp;*.jpg;*.gif;*.png|All files (*.*)|*.*";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    lastDirectory = Path.GetDirectoryName(openFileDialog1.FileName);
                    Registry.SetValue(PocketPictureForm.RegKey, "LastDirectory", lastDirectory);
                    Form.OpenFile(openFileDialog1.FileName);
                }
            }
#endif
            base.DoAction();
        }
    }

    public class SaveAction: Action
    {
        public SaveAction()
        {
            this.Name = "Save";
            this.ImageName = "SaveAction";
            this.ParentName = "File1";
            this.Hint = "Save image";
        }

        public override void DoAction()
        {
            Form.Save();
            base.DoAction();
        }
    }

    public class SaveAsAction: Action
    {
        public SaveAsAction()
        {
            this.Name = "Save as";
            this.ImageName = "SaveAsAction";
            this.ParentName = "File1";
            this.Hint = "Save image as different name";
        }

        public override void DoAction()
        {
            Form.SaveAs();
            base.DoAction();
        }
    }

    public class ChangeSizeAction: Action
    {
        public ChangeSizeAction()
        {
            this.Name = "Change size";
            this.ImageName = "ChangeSizeAction";
            this.ParentName = "File1";
            this.Hint = "Change image size";
        }

        public override void DoAction()
        {
            Form.ChangeSize();
            base.DoAction();
        }
    }

    public class CloseAction: Action
    {
        public CloseAction()
        {
            this.Name = "Quit";
            this.ImageName = "Close";
            this.ParentName = "File2";
            this.Hint = "Exit this program";
        }

        public override void DoAction()
        {
            Form.Close();
            CloseDropDownOnClick = false;
            base.DoAction();
        }
    }

    public class OptionsAction: Action
    {
        public OptionsAction()
        {
            this.Name = "Options...";
            this.ImageName = "Options";
            this.ParentName = "File1";
            this.Hint = "Shows options dialog";
        }

        public override void DoAction()
        {
            PropertyEditors pe = new PropertyEditors();
            pe.EditOnlyMarkedAsEditable = true;
            bool wasFullScreen = Form.FullScreenMode;

            if (pe.EditObject(Form))
            {
                Serialization sr = new Serialization();
                sr.SerializeOnlyMarkedAsSerializable = true;
                MemoryStream s = new MemoryStream();
                sr.SaveObjProperties(Form, new BinaryWriter(s));
                //s.Close();
                Registry.SetValue(PocketPictureForm.RegKey, "OtherOptions", s.ToArray());

                CloseDropDownOnClick = !wasFullScreen;
                Form.UpdateControlCompositionFromProperties();
            }
            else
                CloseDropDownOnClick = true;

            //Form.Close();
            base.DoAction();
        }
    }
}